package com.nucarf.bi.digtalize.bean;

import com.nucarf.bi.digtalize.util.PropertyUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class GlobalConsume {
    /**
     * primary key
     */
    private Integer id;

    /**
     * 统一客户编码
     */
    private Long customer_number;

    /**
     * 企业订单id
     */
    private Integer company_order_id;

    /**
     * 司机订单id
     */
    private Integer driver_order_id;

    /**
     * 订单状态
     */
    private int status;

    /**
     * 油品大类id
     */
    private Integer fuel_type_id;

    /**
     * 油品大类名称
     */
    private String fuel_type_name;

    /**
     * 油品小类id
     */
    private Integer fuel_id;

    /**
     * 油品小类名称
     */
    private String fuel_name;

    /**
     * 1企业2机构3个人（可为空）
     */
    private int order_type;

    /**
     * 企业id
     */
    private Integer company_id;

    /**
     * 企业名称
     */
    private String company_name;

    /**
     * 企业协议主体id
     */
    private int agreement_id;

    /**
     * 企业协议主体名称
     */
    private String agreement_name;

    /**
     * 1企业 2机构 3个人 4车辆
     */
    private int account_type;

    /**
     * 1旧账户 2新账户
     */
    private int acc_type;

    /**
     * 账户id
     */
    private Integer account_id;

    /**
     * 子账户id
     */
    private Integer son_account_id;

    /**
     * 账户金额（单位：分）
     */
    private Integer account_money;

    /**
     * 个人账户id
     */
    private Integer driver_account_id;

    /**
     * 个人子账户id
     */
    private Integer son_driver_account_id;

    /**
     * 个人账户消费金额
     */
    private Integer driver_account_money;

    /**
     * 优惠id
     */
    private Integer discount_id;

    /**
     * 油站id
     */
    private Integer station_id;

    /**
     * 油站名称
     */
    private String station_name;

    /**
     * 油站商户id
     */
    private Integer shop_id;

    /**
     * 油站商户名称
     */
    private String shop_name;

    /**
     * 应收金额
     */
    private Integer need_amt;

    /**
     * 万金油优惠金额
     */
    private Integer gift_amt;

    /**
     * 油卡优惠金额
     */
    private Integer card_gift_amt;

    /**
     * 实收金额
     */
    private Integer recv_amt;

    /**
     * 收取现金金额
     */
    private Integer cash_amt;

    /**
     * 油品单位
     */
    private String fuel_unit;

    /**
     * 单价
     */
    private Integer unit_price;

    /**
     * 加油数量
     */
    private String quantity;

    /**
     * 订单编号
     */
    private String order_sn;

    /**
     * 油卡id
     */
    private Integer cart_id;

    /**
     * 油站账户id
     */
    private Integer mc_id;

    /**
     * 会员id
     */
    private Integer member_id;

    /**
     * 企业账户支付的资金
     */
    private Integer company_amount;

    /**
     * 企业结算资金金额
     */
    private Integer amount;

    /**
     * 原始订单id
     */
    private Integer source_order_id;

    /**
     * 创建时间
     */
    private Integer create_time;

    /**
     * 修改时间
     */
    private Integer update_time;

    /**
     * 调单时间
     */
    private Integer change_time;

    /**
     * 1:正常订单， 2：冲红订单
     */
    private int change_type;

    /**
     * 支付时间
     */
    private Integer pay_time;

    /**
     * 取消时间
     */
    private Integer cancel_time;

    /**
     * 支付类型：1卡支付
     */
    private int pay_type;

    /**
     * 司机姓名
     */
    private String mc_name;

    /**
     * copy 推送数据所需字段
     *
     * @param topic 要发送的 topic
     * @return
     */
    public SinkKafkaBean copyToSinkKafkaBean(String topic, String... defaultValue) {
        if (this.create_time.longValue() <= 1625846400L) return null;

        SinkKafkaBean<PushConsume> pushConsumeSinkKafkaBean = new SinkKafkaBean<>();

        PushConsume pushConsume = new PushConsume();

        String cusCode = null;
        String companyName = null;
        Long customer_number = this.customer_number;
        if (customer_number == null || customer_number.intValue() == 0) {
            cusCode = defaultValue[0];
            companyName = "第三方";
        } else {
            cusCode = customer_number.toString();
            companyName = this.company_name;
        }

        pushConsume.setCus_code(cusCode);
        pushConsume.setTrade_no(this.company_order_id.longValue());
        pushConsume.setSource(1);
        pushConsume.setCompany_name(companyName);

        //油品大类
        pushConsume.setFuel_code(this.fuel_type_id);
        pushConsume.setFuel_name(this.fuel_type_name);
        //油品小类
        pushConsume.setFuel_code_number(this.fuel_id);
        pushConsume.setFuel_code_name(this.fuel_name);

        pushConsume.setAmount(BigDecimal.valueOf(this.recv_amt).divide(BigDecimal.valueOf(100), 4, BigDecimal.ROUND_HALF_UP));
        pushConsume.setUpdate_time(this.update_time.longValue());
        pushConsume.setChange_time(this.change_time.longValue());
        pushConsume.setTrade_time(this.create_time.longValue());
        pushConsume.setPrice(BigDecimal.valueOf(this.unit_price).divide(BigDecimal.valueOf(100), 4, BigDecimal.ROUND_HALF_UP));
        pushConsume.setQuantity(this.quantity);
        pushConsume.setFuel_unit(this.fuel_unit);

        if (this.change_type == 2 && this.status == 0) {
            //正常调单的红冲数据
            pushConsume.setOp_type(3);
        } else if (this.status == 1 && this.change_type == 2) {
            //撤单的红冲数据
            pushConsume.setOp_type(2);
        } else if (this.change_type == 1 && this.status == 0) {
            //正常订单、调单后的订单 =>
            pushConsume.setOp_type(1);
        }

        //TODO 客户编码需要业务方补充
        pushConsumeSinkKafkaBean.setCus_code(cusCode);
        pushConsumeSinkKafkaBean.setTopic(topic);
        pushConsumeSinkKafkaBean.setSource(pushConsume.getSource());
        pushConsumeSinkKafkaBean.setTrade_time(this.create_time.longValue());


        pushConsumeSinkKafkaBean.setSendBean(pushConsume);

        return pushConsumeSinkKafkaBean;
    }
}
